import { defineStore } from "pinia";
import { ref } from "vue";
import { roleGetApi, userLoginApi } from "../api/user";
import router from "../router";

export const useAccount = defineStore("account",()=>{
    // 用户信息
    const userInfo = ref(null)
    const jwttoken = ref('')
    const permitData = ref('')
    // 登录
    const loginAccount = async (payload)=>{
        let res =await userLoginApi(payload)
        console.log(res);
        let {code,token,data} = res.data
        if(code == 1){
            userInfo.value = data
            jwttoken.value = token
            // 使用roleId获取permission
            let permit = await roleGetApi(data.roleId)
            console.log('角色的权限信息',permit);
            permitData.value = permit.data.data[0]
            router.push('/')
        }
    }
    return {
        userInfo,
        jwttoken,
        permitData,
        loginAccount 
    }
},{
    persist:{
        key:"mvvm-account",
        enabled:true,
    }
})